package com.xzhuang.task.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.Scheduler;
import org.quartz.impl.StdScheduler;
import org.springframework.beans.factory.annotation.Autowired;

import com.xzhuang.task.util.SpringContextUtil;

public class CustomServletContextListener implements ServletContextListener {
	
	@Autowired
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("TOMCAT 启动  start");
	}

	
	@Autowired
	public void contextDestroyed(ServletContextEvent sce) {
		ServletContext servletContext = getServletContext(sce);

		try {

			Scheduler scheduler = (StdScheduler) SpringContextUtil.getBean("schedulerFactoryBean");
			scheduler.shutdown(true);

			Thread.sleep(1000);
			System.out.println("关闭TOMCAT 销毁quartz线程");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private ServletContext getServletContext(ServletContextEvent sce) {
		ServletContext servletContext = sce.getServletContext();
		return servletContext;
	}

}
